//
//  SEInquiryHistoryViewController.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/4/20.
//

import UIKit

class SEInquiryHistoryViewController: SEBaseViewController {
    
    private var viewModel = SEInquiryViewModel()
    
    private let inquiryType: SEInquiryType = .history

    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewModel.decodeInquiryModels(type: inquiryType)
        
        viewModel.updateData = { [weak self] status in
            self?.tableView.se_empty?.allowShow = true
            self?.endRefreshing()
            switch status {
            case .success:
                self?.tableView.reloadData()
            case .auth:
                self?.presentLoginViewController(selector: #selector(self?.loadData))
            case .failure: break
            }
        }
    }
    
    override func setupUI() {
        super.setupUI()
        
//        view.addSubview(filterHeader)
//        filterHeader.snp.makeConstraints { (make) in
//            make.top.equalToSuperview().offset(1 + UIScreen.navigationBarMaxY)
//            make.left.right.equalToSuperview()
//            make.height.equalTo(40)
//        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(1 + UIScreen.navigationBarMaxY)
        }
    }

    // MARK: - getter
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .background
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 84
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.se_header = SERefreshHeader { [weak self] in self?.loadData() }
        tableView.se_header?.beginRefreshing()
        tableView.se_footer = SERefreshFooter { [weak self] in self?.loadData(more: true) }
        tableView.se_empty = SEEmptyView { [weak self, tableView] in tableView.se_header?.beginRefreshing() }
        
        tableView.register(SEInquiryCell.nibForCell(),
                           forCellReuseIdentifier: SEInquiryCell.reuseIdentifier())
        
        return tableView
    }()
    
    private lazy var filterHeader: SEFilterHeader = {
        let header = SEFilterHeader(frame: .zero)
        header.delegate = self
        return header
    }()
    
    private var filterMenu: SEFilterHeaderMenuView?
}

extension SEInquiryHistoryViewController {
    @objc private func loadData(more: Bool = false) {
        viewModel.getListOfInquiry(type: inquiryType, more: more)
    }
    
    private func endRefreshing() {
        tableView.se_header?.endRefreshing()
        if viewModel.hasMore == false {
            tableView.se_footer?.endRefreshingWithNoMoreData()
        } else {
            tableView.se_footer?.endRefreshing()
        }
    }
}

// MARK: - UITableViewDataSource

extension SEInquiryHistoryViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEInquiryCell.reuseIdentifier()) as! SEInquiryCell
        cell.update(with: viewModel.viewModel(at: indexPath))
        return cell
    }
}

// MARK: - UITableViewDelegate

extension SEInquiryHistoryViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vm = viewModel.viewModel(at: indexPath)
        pushToChatVC(with: vm)
    }
    
    private func pushToChatVC(with vm: SEInquiryViewModel) {
        let chatVc = SEChatViewController(infoViewModel: vm, inquiryType: inquiryType)
        chatVc.title = vm.inquiryDetail.patientName
        navigationController?.pushViewController(chatVc, animated: true)
    }
}

// MARK: - SEFilterHeaderDelegate

extension SEInquiryHistoryViewController: SEFilterHeaderDelegate {
    func showFilterMenu(_ header: SEFilterHeader, didClicked sender: UIButton) {
        let filterType = SEFilterHeaderFilterType(rawValue: sender.tag - header.btnTag) ?? .unknown
        switch filterType {
        case .unknown:
            break
        default:
            if sender.isSelected {
                filterMenu = UINib.loadViewFrom(SEFilterHeaderMenuView.self)
                filterMenu?.showMenu(in: view, rect: header.frame, menuStyle: filterType.menuStyle)
                filterMenu?.filterMenuClosure = {
                    header.reset(sender)
                }
            } else {
                filterMenu?.dismissMenu()
            }
        }
    }
}
